// import { saveRedirectValue } from '@/api/index';

let WSKHURL;
let preWSKHURL;

let otherHost;
const publicPath = process.env.VUE_APP_PUBLIC_PATH;
const localHost = process.env.VUE_APP_LOCAL_AXIOS_HOST;

if (process.env.VUE_NODE_ENV == 'development' || process.env.VUE_NODE_ENV == 'tm') {
  // 开发环境 local
  otherHost = process.env.VUE_APP_TM_AXIOS_OTHER_HOST;
  WSKHURL = 'https://tm.dgzq.com.cn/wskh/index.html';
  preWSKHURL = 'http://59.36.7.122:84/cgi-bin/wechat/PreOpenManagerAction';
}

if (process.env.VUE_NODE_ENV == 'testmall') {
  // 测试环境
  otherHost = process.env.VUE_APP_TESTMALL_AXIOS_OTHER_HOST;
  WSKHURL = 'https://testmall.dgzq.com.cn/wskh/index.html';
  preWSKHURL = 'http://59.36.7.122:84/cgi-bin/wechat/PreOpenManagerAction';
}

if (process.env.VUE_NODE_ENV == 'production') {
  // 生产环境
  otherHost = process.env.VUE_APP_BUILD_AXIOS_OTHER_HOST;
  WSKHURL = 'https://wskh.dgzq.com.cn/index.html#/before';
  preWSKHURL = 'https://service.dgzq.com.cn/cgi-bin/wechat/PreOpenManagerAction';
}

const saveAndRedirectWt = async (fromPage, fromParam, action) => {
  const ticket = sessionStorage.getItem('ticket') || '';
  const opStation = sessionStorage.getItem('opStation');
  // await saveRedirectValue(url);
  let paramStr = '';
  if (fromParam && typeof fromParam == 'object') {
    Object.keys(fromParam).forEach(key => {
      paramStr += `&${key}=${fromParam[key]}`;
    });
  }

  paramStr = paramStr ? `?${paramStr.substr(1)}&ticket=${ticket}` : `?ticket=${ticket}`;
  const url = encodeURIComponent(window.location.origin + fromPage + paramStr);
  // 跳转网厅 device=otcmall 区别老商城
  window.location.href = `${otherHost}/dz-service/mfront/redirect?action=${action}&device=webview&backUrl=${url}&ticket=${ticket}`;
};

export default {
  install(Vue, options) {
    Vue.prototype.global = {
      publicPath,
      fileURL: `${otherHost}/app/file/get`, // 打开文档
    };
    // action:"updateUserInfo"为跳转至修改个人信息；"risk"为跳转至风险测评,"updateId"为更新身份证
    //         "bankTransfer"为银证转账
    // fromPage:"/fund/productInfo"为基金产品；3为资管产品
    Vue.prototype.toWtUserInfo = (param) => {
      saveAndRedirectWt(param.fromPage, param.fromParam, 'updateUserInfo');
    };
    Vue.prototype.toWtUpdateId = (param) => {
      saveAndRedirectWt(param.fromPage, param.fromParam, 'updateId');
    };
    Vue.prototype.toWtRiskTest = (param) => {
      saveAndRedirectWt(param.fromPage, param.fromParam, 'risk');
    };
    Vue.prototype.toYZZZ = (param) => {
      if (window.KDS_Native && window.KDS_Native.switchYZZZ) {
        window.KDS_Native.switchYZZZ();
      } else {
        saveAndRedirectWt(param.fromPage, param.fromParam, 'bsTransfer');
      }
    };
    // 跳转新开户地址
    Vue.prototype.toWSKH = (param) => {
      const tags = param.split('_');
      if (window.KDS_Native && window.KDS_Native.selfserviceAccountWithParameters) {
        window.KDS_Native.selfserviceAccountWithParameters(tags[0], tags[1], tags[2]);
      } else {
        // 传参顺序：活动id，渠道id，推荐人id
        const paramStr = `activityId=${tags[0]}&channelId=${tags[1]}&recommendId=${tags[2]}`;
        window.location.href = `${WSKHURL}?appKey=zzb&appVersion=1.0&specialSdk=h5&${paramStr}`;
      }
    };
    // 跳转开户预登记
    Vue.prototype.toPreWSKH = (channelId) => {
      window.location.replace(`${preWSKHURL}?channelId=${channelId}`);
    };
    // app内跳转
    Vue.prototype.goRedirectUrl = (url, ticket) => {
      if (!url) {
        return false;
      }
      if (/^[0-9]+$/.test(url)) {
        // 综合搜索
        if (window.KDS_Native && window.KDS_Native.switchDeeplink) {
          window.KDS_Native.switchDeeplink(url);
        } else {
          // 仅支持app内跳转，引导跳转掌证宝app下载页
          window.location.href = 'https://www.dgzq.com.cn/dz-ow/mfront#/downloadapp';
        }
      } else if (url.indexOf('upadv://') > -1) {
        if (window.KDS_Native && window.KDS_Native.jumpToUPInvestment) {
          window.KDS_Native.jumpToUPInvestment(url);
        } else {
          // 仅支持app内跳转，引导跳转掌证宝app下载页
          window.location.href = 'https://www.dgzq.com.cn/dz-ow/mfront#/downloadapp';
        }
      } else if (url.indexOf('openaccount://') > -1) {
        const tag = url.split('openaccount://')[1];
        const tags = tag.split('_');
        if (tags && tags.length === 3) {
          if (window.KDS_Native && window.KDS_Native.selfserviceAccountWithParameters) {
            window.KDS_Native.selfserviceAccountWithParameters(tags[0], tags[1], tags[2]);
          } else {
            // 传参顺序：活动id，渠道id，推荐人id
            const paramStr = `activityId=${tags[0]}&channelId=${tags[1]}&recommendId=${tags[2]}`;
            window.location.replace(`${WSKHURL}?appKey=ths&appVersion=1&${paramStr}`);
          }
        }
      } else {
        const isPdf = url.toLowerCase().indexOf('.pdf') > -1;
        if (isPdf) {
          if (window.KDS_Native && window.KDS_Native.openPDF) {
          // 调用app方法打开pdf
            window.KDS_Native.openPDF(url);
          } else {
            window.location.href = url;
          }
        } else {
          const symbol = String(url).indexOf('?') > -1 ? '&' : '?';
          const ticketSymbol = ticket ? `${symbol}ticket=${ticket}` : '';
          // 传了ticket参数的，才在url后拼接ticket参数
          window.location.href = url + ticketSymbol;
        }
      }
    };
  }
};
